const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin') // 打包前清理dist文件夹
const ProgressBarPlugin = require('progress-bar-webpack-plugin') // 打包进度条
const MiniCssExtractPlugin = require('mini-css-extract-plugin') // 抽离css文件到单独的文件中
const chalk = require('chalk')

module.exports = {
  entry: ['@babel/polyfill', './src/app.js'],
  output: {
    path: path.resolve(__dirname, '../dist'),
    filename: '[name].[hash:10].js'
  },
  resolve: {
    alias: {
      '~': path.resolve(__dirname, '../src'),
      '@ant-design/icons/lib/dist$': path.resolve(__dirname, '../src/components/icon.js')
    }
  },
  devServer: {
    stats: 'minimal', // 控制台信息
    port: 8016,
    historyApiFallback: {
      index: '/index.html'
    }
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'babel-loader',
        options: {
          presets: ['@babel/preset-env']
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.css$/,
        use: [
          {
            loader: MiniCssExtractPlugin.loader,
            options: {
              publicPath: '../../',
              esModule: false, // 不加要使用babel插件，否则会报export 'default' (imported as 'xxx') was not found 警告
            }
          },
          'css-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          {
            loader: 'less-loader',
            options: {
              lessOptions: {
                javascriptEnabled: true
              }
            }
          }
        ]
      },
      {
        test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)$/,
        use: [
          {
            loader: 'url-loader',
            options: {
              name: 'assets/images/[name].[hash:10].[ext]',
              limit: 8192, // 8k以上打包图片，以下返回base64位
              esModule: false // file-loader默认采用ES模块语法, vue打包生成的是Commonjs语法，二者不一致会使img的src变成[object Module]
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: 'assets/css/[hash:10].css'
    }),
    new HtmlWebpackPlugin({
      template: './src/index.html', // html模板
      filename: 'index.html',
      favicon: './favicon.ico'
    }),
    new VueLoaderPlugin(),
    new ProgressBarPlugin({ // 打包进度条
      width: 100,
      format: `App is building [:bar] ${chalk.green.bold(':percent')} (:elapsed seconds) \n`,
      clear: false
    }),
    new CleanWebpackPlugin() // 构建之前清理dist目录 todo 这个插件应该放在plugins数组的最后，因为webpack配置是倒序的（最后配置的最先执行）
  ],
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vue: {
          name: 'vue',
          test: /[\\/]node_modules[\\/]vue[\\/]/,
          priority: -10
        },
        vuex: {
          name: 'vuex',
          test: /[\\/]node_modules[\\/]vuex[\\/]/,
          priority: -10
        },
        'vue-router': {
          name: 'vue-router',
          test: /[\\/]node_modules[\\/]vue-router[\\/]/,
          priority: -10
        },
        'ant-design-vue': {
          name: 'ant-design-vue',
          test: /[\\/]node_modules[\\/]ant-design-vue[\\/]/,
          priority: -10
        },
        'moment': {
          name: 'moment',
          test: /[\\/]node_modules[\\/]moment[\\/]/,
          priority: -10
        },
        'vendors': {
          name: 'vendors',
          test: /[\\/]node_modules[\\/]/,
          priority: -20
        }
      }
    }
  }
}
